package com.xej.taco_cloud.controller;

import com.xej.taco_cloud.pojo.User;
import com.xej.taco_cloud.security.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.security.Principal;

//@Controller
public class OrderController {


    @Autowired
    private UserRepository userRepository;

    @PostMapping("/order1/{id}")
    public String order1(Principal principal){
        String name = principal.getName();
        User user = userRepository.findByUserName(name);
        return user.toString();
    }


    @PostMapping("/order2")
    public String order2(Authentication authentication){
        User user = (User) authentication.getPrincipal();
        return user.toString();
    }


    @PostMapping("/order3")
    public String order3(@AuthenticationPrincipal User user){
        return user.toString();
    }


    @PostMapping("/order4")
    public String order4(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        return user.toString();
    }
}